#include<iostream>
#include<math.h>
#define N 20;
using namespace std;
int q[20];			
int count_1 = 0;		
void dispasolution(int n)
{
	cout << "第" << ++count_1 << "个解：";
	for (int i = 1; i <= n; ++i) {
		cout << "(" << i << "," << q[i] << ") ";
	}
	cout << endl;
}
 
bool place(int i, int j)
{
	if (i == 1) return true;
	int k = 1;
	while (k < i) 
    {
		if (q[k] == j || (abs(i - k) == abs(j - q[k])))
        {
			return false;
		}
		k++;
	}
	return true;
}
 
void queen(int i, int n) 
{
	if (i > n) dispasolution(n);	 
	else 
    {
		for (int j = 1; j <= n; ++j) 
        {
			if (place(i, j)) 
            {		 
				q[i] = j;			  
				queen(i + 1, n);	  
			}
		}
	}
}
 
int main() 
{
	int n;
	cout << "皇后问题(n<20)n=";
	cin >> n;
	if (n >= 20) cout << "n值太大,不能求解" << endl;
	else cout << n << "皇后问题求解如下：" << endl;
	queen(1, n);		
    return 0;
}
